//
//  MovieSecondViewController.m
//  UI_豆瓣扩展
//
//  Created by dllo on 15/11/8.
//  Copyright © 2015年 张磊. All rights reserved.
//

#import "MovieSecondViewController.h"
#import "MovieDeteliViewController.h"
#import "AppTools.h"
#import "Movie.h"
#import "MovieCollectionViewCell.h"

@interface MovieSecondViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, retain)UICollectionView *myCollectView;
@property (nonatomic, retain)NSMutableArray *movieArray;
@end

@implementation MovieSecondViewController
- (void)dealloc
{
    [_myCollectView release];
    [_movieArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = CGSizeMake(100, 150);
    flowLayout.minimumInteritemSpacing = 8;
    flowLayout.minimumLineSpacing = 25;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 0, 95, 30);
    
    self.myCollectView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectView];
    self.myCollectView.delegate = self;
    self.myCollectView.dataSource = self;
    [self.myCollectView release];
    
    //注册cell
    [self.myCollectView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    self.movieArray = [NSMutableArray array];
    //请求网络
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSString *reason = [dic objectForKey:@"reason"];
        NSMutableArray *array = [dic objectForKey:@"result"];
        
        if ([reason isEqualToString:@"success"]) {
            for (NSMutableDictionary *Dic in array) {
                Movie *movie = [[Movie alloc]init];
                [movie setValuesForKeysWithDictionary:Dic];
                
                [self.movieArray addObject:movie];
                [movie release];
            }
            
            [self.myCollectView reloadData];
        }else{
            NSLog(@"请求失败");
        }
    }];

    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.movieArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInd = @"cell";
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellInd forIndexPath:indexPath];
    
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    
    cell.aMovie = movie;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDeteliViewController *movieDVC = [[MovieDeteliViewController alloc]init];
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    movieDVC.movieID = movie.movieId;
    [self.navigationController pushViewController:movieDVC animated:YES];
    [movieDVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
